﻿using HandyControl.Tools;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;

namespace WpfApp.Views
{
    public enum TopTipWindowPositions
    {
        TopLeft,
        TopCenter,
        TopRight,
        Hide,
    }

    /// <summary>
    /// TopTipWindow.xaml 的交互逻辑
    /// </summary>
    public partial class TopTipWindow : Window
    {
        private TopTipWindowPositions _position;
        private readonly Timer Timer;

        public TopTipWindow()
        {
            this.Loaded += TopTipWindow_Loaded;
            _position = TopTipWindowPositions.TopLeft;

            Timer = new Timer((obj) =>
            {
                this.Dispatcher.Invoke(() =>
                {
                    this.BringToTop();
                });
            }, null, TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(5));

            InitializeComponent();
            Closed += TopTipWindow_Closed;
        }

        private void TopTipWindow_Loaded(object sender, RoutedEventArgs e)
        {
            // 禁用窗口命中
            var hwnd = this.GetHandle();
            var exStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
            SetWindowLong(hwnd, GWL_EXSTYLE, exStyle | WS_EX_TRANSPARENT | WS_EX_TOOLWINDOW);
        }

        private async void TopTipWindow_Closed(object? sender, EventArgs e)
        {
            await Timer.DisposeAsync();
        }

        // 方法2：使用Win32 API（更可靠）
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

        private void BringToTop()
        {
            var handle = new System.Windows.Interop.WindowInteropHelper(this).Handle;
            SetWindowPos(handle, new IntPtr(-1), 0, 0, 0, 0, 0x0001 | 0x0002 | 0x0010); // SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE
        }

        public void ShowTopLeft()
        {
            if (_position != TopTipWindowPositions.TopLeft || this.Visibility == Visibility.Hidden)
            {
                _position = TopTipWindowPositions.TopLeft;
                Visibility = Visibility.Visible;
                this.Top = 0;
                this.Left = 0;
            }
        }

        public void ShowTopCenter()
        {
            if (_position != TopTipWindowPositions.TopCenter || this.Visibility == Visibility.Hidden)
            {
                _position = TopTipWindowPositions.TopCenter;
                Visibility = Visibility.Visible;
                this.Top = 0;
                this.Left = SystemParameters.WorkArea.Size.Width / 2 - this.Width / 2;
            }
        }

        public void ShowTopRight()
        {
            if (_position != TopTipWindowPositions.TopRight || this.Visibility == Visibility.Hidden)
            {
                _position = TopTipWindowPositions.TopRight;
                Visibility = Visibility.Visible;
                var left = SystemParameters.WorkArea.Size.Width - this.Width;
                this.Top = 0;
                this.Left = left < 0 ? 0 : left;
            }
        }

        public void HideTip()
        {
            this.Visibility = Visibility.Hidden;
            _position = TopTipWindowPositions.Hide;
        }

        private const int WS_EX_TRANSPARENT = 0x00000020;
        private const int WS_EX_TOOLWINDOW = 0x00000080;
        private const int GWL_EXSTYLE = (-20);

        [DllImport("user32.dll")]
        private static extern int SetWindowLong(IntPtr hwnd, int nIndex, int dwNewLong);

        [DllImport("user32.dll")]
        private static extern int GetWindowLong(IntPtr hwnd, int nIndex);
    }
}
